package com.tsinghua.server.base.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.MapDifference;
import com.google.common.collect.Maps;
import com.tsinghua.server.common.Result;
import com.tsinghua.server.base.service.Fxct1914ztzbService;
import com.tsinghua.server.common.utils.StringUtil;
import com.tsinghua.server.original.mapper.FxjcCtzMapper;
import com.tsinghua.server.original.mapper.FxjcCyzMapper;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping(value = "/risk/dashboard")
@RequiredArgsConstructor
public class DashBoardController {

    @Resource
    private final Fxct1914ztzbService fxct1914ztzbService;

    private final FxjcCtzMapper fxjcCtzMapper;
    private final FxjcCyzMapper fxjcCyzMapper;

    @GetMapping(value = "ctchart1")
    public Result listDashBoard() {
        return Result.succ(fxct1914ztzbService.getProvinceNum());
    }

    @GetMapping(value = "ctchart2/{tagId}")
    public Result listBenefitBoard(@PathVariable Integer tagId) {
        return Result.succ(fxct1914ztzbService.getBenefitBoard(tagId));
    }

    @GetMapping(value = "jrchart3")
    public Result jrchart3() {
        return Result.succ(true);
    }


    /**
     * 首页获取图表
     * @param type
     * @param syl
     * @param hy
     * @return
     */
    @GetMapping(value = "data")
    public Result dataArray(@RequestParam(name = "type", required = false) Integer type,
                            @RequestParam(name = "syl",required = false) String syl,
                            @RequestParam(name = "hy",required = false) String hy){

        Map<String,Object> result = new HashMap<>();
        List<Map<String,Object>> listData = new ArrayList<>();
        List<Map<String,Object>> sylList = new ArrayList<>();

        if (type == 1) {
            String tableName = "fxjc_ctz";
             listData = fxjcCtzMapper.findProvinces(tableName);
             if(StringUtils.isNotEmpty(syl)){
                 sylList = fxjcCtzMapper.findNums(tableName,syl);
                 result.put("sylList",sylList);
             }
            result.put("listData",listData);
            return Result.succ(result);
        }

        if (type == 2) {
            String tableName = "fxjc_cyz";
             listData = fxjcCyzMapper.findProvinces(tableName);
             sylList = fxjcCtzMapper.findNumsByCyz(tableName);
            result.put("sylList",sylList);
            result.put("listData",listData);
            return Result.succ(result);
        }

        if (type == 3) {
            String tableName = "fxjc_jrz";
            listData = fxjcCyzMapper.findProvinces(tableName);
            if (StringUtils.isNotEmpty(hy)){
                sylList = fxjcCtzMapper.findNumsByJrz(tableName,hy);
                result.put("sylList",sylList);
            }
            result.put("listData",listData);

            return Result.succ(result);

        }
        return Result.succ(result);
    }




    public static void list(List<Map<String,Object>> list1, List<Map<String,Object>> list2){
        List<Map<String,Object>> differentList = list1.stream().filter((item) -> !list2.stream().map((item2) -> item2.get("sf_zt")).collect(Collectors.toList()).contains(item.get("sf_zt"))).collect(Collectors.toList());
        if (CollectionUtil.isNotEmpty(differentList)) {
            for( Map<String, Object> mapList : differentList ) {
                for (String key : mapList.keySet()) {
                    if (key.equals("num")){
                        mapList.put(key, 0);
                    }
                }
            }
        }
        list2.addAll(differentList);

    }


}
